home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 July: Mac OS SDK / Dev.CD Jul 96 SDK / Dev.CD Jul 96 SDK1.toast / Development Kits (Disc 1) / OpenDoc Development Framework / ODFDev / ODF / Found / FWMemory / Sources / FWMemHlp.cpp < prev    next >
Encoding:
Text File  |  1996-04-25  |  7.2 KB  |  230 lines  |  [TEXT/MPS ]

  1. //========================================================================================
  2. //
  3. //    File:                FWMemHlp.cpp
  4. //    Release Version:    $ ODF 1 $
  5. //
  6. //    Copyright:    (c) 1993 - 1996 by Apple Computer, Inc., all rights reserved.
  7. //
  8. //========================================================================================
  9.  
  10. #include "FWFound.hpp"
  11.  
  12. #ifndef   FWMEMHLP_H
  13. #include "FWMemHlp.h"
  14. #endif
  15.  
  16. #ifndef   FWMEMMGR_H
  17. #include "FWMemMgr.h"
  18. #endif
  19.  
  20. #ifndef   FWPRIDEB_H
  21. #include "FWPriDeb.h"
  22. #endif
  23.  
  24. #ifdef FW_BUILD_MAC
  25. #pragma segment FWMemory
  26. #endif
  27.  
  28. //========================================================================================
  29. //    class FW_CAcquireLockedSystemHandle
  30. //========================================================================================
  31.  
  32. FW_DEFINE_AUTO(FW_CAcquireLockedSystemHandle)
  33.  
  34. //----------------------------------------------------------------------------------------
  35. // FW_CAcquireLockedSystemHandle::FW_CAcquireLockedSystemHandle
  36. //----------------------------------------------------------------------------------------
  37.  
  38. FW_CAcquireLockedSystemHandle::FW_CAcquireLockedSystemHandle(FW_PlatformHandle aSystemHandle)
  39.                      : fLockedSystemHandle(aSystemHandle) 
  40. {
  41. #ifdef FW_BUILD_MAC
  42.     fLockState = ::HGetState(aSystemHandle);
  43. #endif
  44.     fLockedPointer = FW_CMemoryManager::LockSystemHandle(aSystemHandle);
  45.     FW_END_CONSTRUCTOR
  46. }
  47.  
  48. //----------------------------------------------------------------------------------------
  49. // FW_CAcquireLockedSystemHandle::~FW_CAcquireLockedSystemHandle
  50. //----------------------------------------------------------------------------------------
  51.  
  52. FW_CAcquireLockedSystemHandle::~FW_CAcquireLockedSystemHandle()
  53. {
  54.     FW_START_DESTRUCTOR
  55. #ifdef FW_BUILD_MAC
  56.     ::HSetState(fLockedSystemHandle, fLockState);
  57. #else
  58.     FW_CMemoryManager::UnlockSystemHandle(fLockedSystemHandle);
  59. #endif
  60. }
  61.  
  62. //========================================================================================
  63. // CLASS FW_CAcquireTemporarySystemHandle
  64. //========================================================================================
  65.  
  66. FW_DEFINE_AUTO(FW_CAcquireTemporarySystemHandle)
  67.  
  68. //----------------------------------------------------------------------------------------
  69. // FW_CAcquireTemporarySystemHandle::FW_CAcquireTemporarySystemHandle
  70. //----------------------------------------------------------------------------------------
  71.  
  72. FW_CAcquireTemporarySystemHandle::FW_CAcquireTemporarySystemHandle(unsigned long size) :
  73.     fTemporarySystemHandle(FW_CMemoryManager::AllocateSystemHandle(size))
  74. {
  75.     FW_TRY
  76.     {
  77.         fMemoryPointer = FW_CMemoryManager::LockSystemHandle(fTemporarySystemHandle);
  78.         fFree = TRUE;
  79.     }
  80.     FW_CATCH_BEGIN
  81.     FW_CATCH_EVERYTHING()
  82.     {
  83.         FW_CMemoryManager::FreeSystemHandle(fTemporarySystemHandle);
  84.         FW_THROW_SAME();
  85.     }
  86.     FW_CATCH_END
  87.     
  88.     FW_END_CONSTRUCTOR
  89. }
  90.  
  91. //----------------------------------------------------------------------------------------
  92. // FW_CAcquireTemporarySystemHandle::Orphan
  93. //----------------------------------------------------------------------------------------
  94.  
  95. FW_PlatformHandle FW_CAcquireTemporarySystemHandle::Orphan()
  96. {
  97.     FW_ASSERT(fFree == TRUE);
  98.     fFree = FALSE;
  99.     
  100.     FW_CMemoryManager::UnlockSystemHandle(fTemporarySystemHandle);
  101.     fMemoryPointer = NULL;
  102.  
  103.     return fTemporarySystemHandle;
  104. }
  105.  
  106. //----------------------------------------------------------------------------------------
  107. // FW_CAcquireTemporarySystemHandle::~FW_CAcquireTemporarySystemHandle
  108. //----------------------------------------------------------------------------------------
  109.  
  110. FW_CAcquireTemporarySystemHandle::~FW_CAcquireTemporarySystemHandle(void)
  111. {
  112.     FW_START_DESTRUCTOR
  113.     
  114.     if(fFree)
  115.     {
  116.         FW_CMemoryManager::UnlockSystemHandle(fTemporarySystemHandle);
  117.         FW_CMemoryManager::FreeSystemHandle(fTemporarySystemHandle);
  118.     }
  119. }
  120.  
  121.  
  122. //----------------------------------------------------------------------------------------
  123. // FW_CAcquireTemporarySystemHandle::Resize
  124. //----------------------------------------------------------------------------------------
  125.  
  126. void* FW_CAcquireTemporarySystemHandle::Resize(unsigned long newSize)
  127. {
  128.     // unlock the handle before resizing
  129.     FW_CMemoryManager::UnlockSystemHandle(fTemporarySystemHandle);
  130.     
  131.     // then resize it
  132.     FW_TRY
  133.     {
  134.         fTemporarySystemHandle =  FW_CMemoryManager::ResizeSystemHandle(fTemporarySystemHandle, newSize);
  135.         fMemoryPointer = FW_CMemoryManager::LockSystemHandle(fTemporarySystemHandle);
  136.     }
  137.     FW_CATCH_BEGIN
  138.     FW_CATCH_EVERYTHING()
  139.     {
  140.         // relock the handle
  141.         FW_CMemoryManager::LockSystemHandle(fTemporarySystemHandle);
  142.         FW_THROW_SAME();
  143.     }
  144.     FW_CATCH_END
  145.     
  146.     return GetPointer();
  147. }
  148.  
  149. //========================================================================================
  150. // CLASS FW_CAcquireTemporaryMemory
  151. //========================================================================================
  152.  
  153. FW_DEFINE_AUTO(FW_CAcquireTemporaryMemory)
  154.  
  155. //----------------------------------------------------------------------------------------
  156. // FW_CAcquireTemporaryMemory::FW_CAcquireTemporaryMemory
  157. //----------------------------------------------------------------------------------------
  158.  
  159. FW_CAcquireTemporaryMemory::FW_CAcquireTemporaryMemory(unsigned long size)
  160. {
  161.     fPointer = FW_CMemoryManager::AllocateBlock(size);
  162.     FW_END_CONSTRUCTOR
  163. }
  164.  
  165. //----------------------------------------------------------------------------------------
  166. // FW_CAcquireTemporaryMemory::~FW_CAcquireTemporaryMemory
  167. //----------------------------------------------------------------------------------------
  168.  
  169. FW_CAcquireTemporaryMemory::~FW_CAcquireTemporaryMemory()
  170. {
  171.     FW_START_DESTRUCTOR
  172.     FW_CMemoryManager::FreeBlock(fPointer);
  173. }
  174.  
  175. //----------------------------------------------------------------------------------------
  176. // FW_CAcquireTemporaryMemory::OrphanPointer
  177. //----------------------------------------------------------------------------------------
  178.  
  179. void* FW_CAcquireTemporaryMemory::OrphanPointer()
  180. {
  181.     FW_ASSERT(fPointer != NULL);        // Only orphan once
  182.     void* t = fPointer;
  183.     fPointer = NULL;
  184.     return t;
  185. }
  186.  
  187. #ifdef FW_BUILD_MAC
  188. //========================================================================================
  189. // CLASS FW_CMacAcquireMultiFinderHeapZone
  190. //========================================================================================
  191.  
  192. FW_DEFINE_AUTO(FW_CMacAcquireMultiFinderHeapZone)
  193.  
  194. //----------------------------------------------------------------------------------------
  195. // FW_CMacAcquireMultiFinderHeapZone::FW_CMacAcquireMultiFinderHeapZone
  196. //----------------------------------------------------------------------------------------
  197.  
  198. FW_CMacAcquireMultiFinderHeapZone::FW_CMacAcquireMultiFinderHeapZone() :
  199.     fOldHeap(NULL)
  200. {
  201.     OSErr heapError;
  202.             
  203.     Handle heapTestHandle = ::TempNewHandle( 1, &heapError );
  204.     THz tempHeap = ::HandleZone( heapTestHandle );
  205.     heapError = ::MemError( );
  206.     ::DisposeHandle( heapTestHandle );
  207.     
  208.     if ( (heapError == noErr) && tempHeap ) {
  209.         fOldHeap = ::GetZone( );
  210.         ::SetZone( tempHeap );
  211.     }
  212.  
  213.     FW_END_CONSTRUCTOR
  214. }
  215.  
  216. //----------------------------------------------------------------------------------------
  217. // FW_CMacAcquireMultiFinderHeapZone::~FW_CMacAcquireMultiFinderHeapZone
  218. //----------------------------------------------------------------------------------------
  219.  
  220. FW_CMacAcquireMultiFinderHeapZone::~FW_CMacAcquireMultiFinderHeapZone()
  221. {
  222.     FW_START_DESTRUCTOR
  223.     
  224.     if ( fOldHeap != NULL )
  225.         ::SetZone( fOldHeap );
  226. }
  227. #endif
  228.  
  229.  
  230.